//
//  CCitySelector.m
//  HCHK_iphone
//
//  Created by yilong zhang on 12-4-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CCitySelector.h"

@implementation CCitySelector

@synthesize delegate;
@synthesize m_TopBar;
@synthesize m_NaviItem;
@synthesize m_Title;
@synthesize m_enShowGrade;
@synthesize m_MapView;
@synthesize m_City;
@synthesize m_ComitBtn;
@synthesize m_LoadView;
@synthesize m_LoadBack;
@synthesize m_Indicator;

-(id)initWithFrame:(CGRect)frame ShowGrade:(ShowGrade)showgrade
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        self.m_enShowGrade = showgrade;
        
        m_MapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 46, frame.size.width, frame.size.height-46)];
        float showdic = 0.0;
        switch (m_enShowGrade)
        {
            case ToCountry:
                showdic = 10000000;
                break;
            case ToProvince:
                showdic = 1000000;
                break;
            case ToCity:
                showdic = 100000;
                break;
            case ToStreet:
                showdic = 10000;
                break;
            default:
                showdic = 10000;
                break;
        }
        m_MapView.region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(34.284, 108.947), showdic, showdic);
        m_MapView.showsUserLocation = YES;
        [self addSubview:m_MapView];
        [m_MapView release];
        
        //m_TopBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 51)];
        //m_TopBar.barStyle = UIBarStyleBlackOpaque;
        //m_TopBar.clipsToBounds = NO;
        //[self addSubview:m_TopBar];
        //[m_TopBar release];
        UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 51)];
        view.image = [UIImage imageNamed: @"barbg.png"];
        [self addSubview:view];
        [view release];
        
        //m_NaviItem = [[UINavigationItem alloc] initWithTitle:nil];
        //[m_TopBar pushNavigationItem:m_NaviItem animated:NO];
        //[m_NaviItem release];
        
        m_Title = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-70)/2, 16, 70, 18)];
        m_Title.image = [UIImage imageNamed:@"maptitle.png"];
        //m_Title.backgroundColor = [UIColor clearColor];
        //m_Title.textAlignment = UITextAlignmentCenter;
        //m_NaviItem.titleView = m_Title;
        [self addSubview:m_Title];
        [m_Title release];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(frame.size.width-80, 6, 70, 39);
        //btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setBackgroundImage:[UIImage imageNamed:@"commitcom.png"] forState:UIControlStateNormal];
        //[btn setTitle:@"完成" forState:UIControlStateNormal];
        //[btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(OnFinishedDown:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 0;
        [self addSubview:btn];
        //m_NaviItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
        
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10, 6, 70, 39);
        //btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setBackgroundImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
        //[btn setTitle:@"取消" forState:UIControlStateNormal];
        //[btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(OnCancelDown:) forControlEvents:UIControlEventTouchUpInside];
        //m_NaviItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
        btn.tag = 1;
        [self addSubview:btn];
        
        m_City = [[UILabel alloc] initWithFrame:CGRectMake(10, view.frame.size.height, view.frame.size.width-50, 30)];
        m_City.numberOfLines = 2;
        m_City.lineBreakMode = UILineBreakModeWordWrap;
        m_City.font = [UIFont systemFontOfSize:12];
        m_City.backgroundColor = [UIColor colorWithRed:0.90 green:0.87 blue:0.79 alpha:1];
        m_City.layer.cornerRadius = 5;
        m_City.layer.borderWidth  = 1;
        m_City.layer.borderColor  = [[UIColor lightGrayColor] CGColor];
        m_City.text = @" 位置信息:";
        [self addSubview:m_City];
        [m_City release];
        
        UIImageView *point = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
        point.backgroundColor = [UIColor clearColor];
        point.center = CGPointMake(frame.size.width/2, frame.size.height/2);
        point.image = [UIImage imageNamed:@"anchor.png"];
        [self addSubview:point];
        [point release];
        
        m_ComitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_ComitBtn.frame = CGRectMake((frame.size.width-164)/2, frame.size.height-41, 164, 31);
        //[m_ComitBtn setTitle:@"我在这里" forState:UIControlStateNormal];
        //[m_ComitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [m_ComitBtn setBackgroundImage:[UIImage imageNamed:@"curpositon.png"] forState:UIControlStateNormal];
        m_ComitBtn.alpha = 0.9;
        [m_ComitBtn addTarget:self action:@selector(OnComitButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:m_ComitBtn];
        
        m_LoadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
        m_LoadView.backgroundColor = [UIColor clearColor];
        m_LoadView.center = CGPointMake(frame.size.width/2, frame.size.height/2);
        m_LoadView.alpha  = 0.0;
        [self addSubview:m_LoadView];
        [m_LoadView release];
        
        m_LoadBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, m_LoadView.frame.size.width, m_LoadView.frame.size.height)];
        m_LoadBack.backgroundColor = [UIColor blackColor];
        m_LoadBack.layer.cornerRadius = 5;
        m_LoadBack.alpha = 0.8;
        [m_LoadView addSubview:m_LoadBack];
        [m_LoadBack release];
        
        m_Indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        m_Indicator.frame = CGRectMake(0, 0, 30, 30);
        m_Indicator.center= CGPointMake(m_LoadView.frame.size.width/2, m_LoadView.frame.size.height/2);
        m_Indicator.hidesWhenStopped = YES;
        [m_LoadView addSubview:m_Indicator];
        [m_Indicator release];
    }
    
    return self;
}

-(void)dealloc
{
    [super dealloc];
}

-(void)OnComitButtonDown:(UIButton*)sender
{
    //if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"cloudscan://"]])
    //if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"imap://[<user>[;AUTH=<type>]@]<host>[:<port>]/<command>"]])
    /*if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"imap://"]])
    {
        NSLog(@"Can't opne.");
    }
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?q=%f,%f", m_MapView.centerCoordinate.latitude, m_MapView.centerCoordinate.longitude]];  
    [[UIApplication sharedApplication]openURL:url]; 
    
    return;*/
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ListenLocation:) name:MSG_LOCATION_RET object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ListenLocation:) name:MSG_LOCATION_TIMEOUT object:nil];

    m_City.text = @" 当前位置:";
    m_ComitBtn.enabled = NO;
    m_LoadView.alpha = 1.0;
    [m_Indicator startAnimating];
    [[CLocator DefaultLocator] GetAddressInfoWithParams:zhHans Coordinate:m_MapView.centerCoordinate];
}
                                         
-(void)OnFinishedDown:(UIButton*)sender
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_LOCATION_RET object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_LOCATION_TIMEOUT object:nil];
    
    [delegate DidCitySelectorActived:sender.tag Object:self];
}

-(void)OnCancelDown:(UIButton*)sender
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_LOCATION_RET object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_LOCATION_TIMEOUT object:nil];
    
    [delegate DidCitySelectorActived:sender.tag Object:self];
}

-(void)ListenLocation:(NSNotification*)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_LOCATION_RET object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_LOCATION_TIMEOUT object:nil];
    
    m_LoadView.alpha = 0.0;
    [m_Indicator stopAnimating];
    m_ComitBtn.enabled = YES;
    
    if (!notification.object)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络繁忙,稍后再试..." delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [alter show];
        [alter autorelease];
    }
    else 
    {
        CAddrInfo *info = notification.object;
        switch (m_enShowGrade)
        {
            case ToCountry:
                m_City.text = [NSString stringWithFormat:@" 当前位置:%@", info.m_strCountry];
                break;
            case ToProvince:
                if (![info.m_strProvince isEqualToString:info.m_strCountry])
                {
                    m_City.text = [NSString stringWithFormat:@" 当前位置:%@ %@", info.m_strCountry, info.m_strProvince];
                }
                else 
                {
                    m_City.text = [NSString stringWithFormat:@" 当前位置:%@", info.m_strCountry];
                }
                break;
            case ToCity:
                if (![info.m_strCity isEqualToString:info.m_strProvince])
                {
                    m_City.text = [NSString stringWithFormat:@" 当前位置:%@ %@ %@", info.m_strCountry, info.m_strProvince, info.m_strCity];
                }
                else 
                {
                    if (![info.m_strProvince isEqualToString:info.m_strCountry])
                    {
                        m_City.text = [NSString stringWithFormat:@" 当前位置:%@ %@", info.m_strCountry, info.m_strProvince];
                    }
                    else 
                    {
                        m_City.text = [NSString stringWithFormat:@" 当前位置:%@", info.m_strCountry];
                    }
                }
                break;
            case ToStreet:
                m_City.text = [NSString stringWithFormat:@" 当前位置:%@", info.m_strSum];
                break;
            default:
                break;
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
